package org.bonorsoft.modules.wechat.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.control.entity.Staff;
import org.bonorsoft.modules.control.service.ICommonService;
import org.bonorsoft.modules.control.service.IStaffService;
import org.bonorsoft.modules.control.service.IUnitService;
import org.bonorsoft.modules.control.vo.StaffVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 小程序端从业人员信息
 */
@Slf4j
@Controller
@RequestMapping("/wechat/staff")
public class WxStaffController  extends BonorController<Staff, IStaffService> {

    @Autowired
    private IStaffService staffService;

    @Autowired
    private ICommonService commonService;

    /**
     * 从业人口分页列表查询
     *
     * @param staff
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "实有人口从业状况(从业人员信息）-分页列表查询")
    @RequiresPermissions("wechat:staff:list")
    @GetMapping(value = "/list")
    @ResponseBody
    public Result<?> queryPageList(Staff staff,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<Staff> queryWrapper = QueryGenerator.initQueryWrapper(staff, req.getParameterMap());
        Page<Staff> page = new Page<Staff>(pageNo, pageSize);
        IPage<StaffVo> pageList = staffService.paging(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 添加从业人员
     *
     * @param staff
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> add(@RequestBody StaffVo staff) {
        String photo = staff.getPhoto();
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        staffService.save(staff.setId(id));
        if(oConvertUtils.isNotEmpty(photo)){
            commonService.addPhoto(id, photo);
        }
        return Result.ok("添加成功！");
    }

    /**
     * 从业人员编辑
     *
     * @param staff
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Result<?> edit(@RequestBody StaffVo staff) {
        String photo = staff.getPhoto();
        staff.setGxsj(new Date());
        staffService.updateById(staff);
        if(oConvertUtils.isNotEmpty(photo)){
            commonService.updatePhoto(staff.getId(), photo);
        }
        return Result.ok("编辑成功!");
    }

    /**
     * 从业人员详细
     * @param req
     * @return
     */
    @GetMapping("/detail")
    @ResponseBody
    public Result<?> detail(HttpServletRequest req){
        String id = req.getParameter("id");
        Result<JSONObject> result = new Result<>();
        JSONObject obj = new JSONObject();
        Staff sta = staffService.getById(id);
        StaffVo stavo = new StaffVo();
        BeanUtils.copyProperties(sta,stavo);
        stavo.setPhoto(commonService.getPhoto(id));
        obj.put("detail",stavo);
        result.setResult(obj);
        result.success("查询成功！");
        return result;
    }

}
